package com.ben.xinyue.seckill.service;

import com.ben.xinyue.seckill.domain.entity.Goods;
import com.ben.xinyue.seckill.domain.vo.GoodsVo;
import com.ben.xinyue.seckill.mapper.GoodsMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class GoodsService  {

    private final GoodsMapper goodsMapper;

    /**
     * 查询秒杀商品数据
     * @return 结果
     */
    public List<GoodsVo> listGoodsVo(){
        return goodsMapper.listGoodsVo();
    }

    /**
     * 根据ID 查询商品数据
     * @param goodsId 商品ID
     * @return 结果
     */
    public GoodsVo queryGoodsVoByGoodsId(long goodsId) {
        return goodsMapper.selectGoodsVoByGoodsId(goodsId);
    }

    /**
     * 减库存
     * @param goodsVo 商品
     */
    public void reduceStock(GoodsVo goodsVo) {
        Goods goods = new Goods();
        goods.setId(goodsVo.getId());
        goods.setGoodsStock(goodsVo.getGoodsStock()-1);
        goodsMapper.updateById(goods);
    }
}